﻿using LightWadf.Core.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LightWadf.Core.Route
{
    public class DoubleRouteParameter : IRouteParameter
    {
        private double defaultValue = double.MinValue;
        private double min = double.MinValue;
        private double max = double.MaxValue;

        public DoubleRouteParameter()
        {
        }

        public DoubleRouteParameter(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                Regex regex = new Regex(@",([dlr])\[([^\]]+)\]");
                MatchCollection matchs = regex.Matches(str);
                foreach (Match match in matchs)
                {
                    switch (match.Groups[1].Value)
                    {
                        case "d":
                            try
                            {
                                this.defaultValue = double.Parse(match.Groups[2].Value);
                            }
                            catch (Exception e)
                            {
                                throw new TypeInitializationException("LightWadf.Core.Route.DoubleRouteItem", new Exception("在初始化 DoubleRouteItem 的默认值时，无法将 string 转换为 double"));
                            }
                            break;
                        case "r":
                            string range = match.Groups[2].Value;
                            string[] len = range.Split(',');
                            if (len.Length != 2)
                            {
                                throw new TypeInitializationException("LightWadf.Core.Route.DoubleRouteItem", new Exception("double 类型的路由的 range(r) 属性格式应为:r[0.0,1.0]，不能有多余符号"));
                            }
                            else
                            {
                                try
                                {
                                    this.min = double.Parse(len[0]);
                                    this.max = double.Parse(len[1]);
                                }
                                catch (Exception e)
                                {
                                    throw new TypeInitializationException("LightWadf.Core.Route.DoubleRouteItem", new Exception("在初始化 DoubleRouteItem 的 range(r) 属性时，无法将 string 转换为 double"));
                                }
                            }
                            break;
                    }
                }
            }
        }

        public Tuple<bool, object> Match(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                try
                {
                    double value = double.Parse(str);
                    if (value >= this.min && value <= this.max)
                    {
                        return Tuple.Create<bool, object>(true, value);
                    }
                }
                catch (Exception e)
                {
                }
            }
            if (this.defaultValue != double.MinValue)
            {
                return Tuple.Create<bool, object>(true, this.defaultValue);
            }
            else
            {
                return Tuple.Create<bool, object>(false, null);
            }
        }
    }
}
